﻿//
//
/////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Threading;
using FreeSpace.Core;
using FreeSpace.Logic;
using FreeSpace.Logic.Worker;
using FreeSpaceLibrary;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

/////////////////////////////////////////////////////////////////////////////
namespace FreeSpace.Scenes
{
    /////////////////////////////////////////////////////////////////////////
    public class LoadScene : BaseScene
    {
        private string _text = "Loading...";
        private SpriteFont _fontSprite;
        private SpriteBatch _sprite;
        private Vector2 _position;
        private GraphicsDevice _device;
        private ContentManager _contentManager;
        private TimeSpan _lastChangeTimeInterval;
        //private AsteroidFieldControl _asteroidFieldManager;
        //private FieldControlLoader _fieldControlLoader;
        //private Thread _fieldControlLoaderThread;

        /////////////////////////////////////////////////////////////////////
        public GraphicsDevice Device
        {
            get { return _device; }
            set { _device = value; }
        }

        /////////////////////////////////////////////////////////////////////
        public ContentManager ContentManager
        {
            get { return _contentManager; }
            set { _contentManager = value; }
        }

        /////////////////////////////////////////////////////////////////////
        public LoadScene(SpriteBatch sprite, GraphicsDevice device, ContentManager contentManager)
            :base(sprite)
        {
            _lastChangeTimeInterval = new TimeSpan(0);

            _sprite = sprite;
            _device = device;
            _contentManager = contentManager;

            _fontSprite = ContentLoader.ContentFonts[FontEnum.LoadingCaption];
            _position = new Vector2(50, 50);
        }

        /////////////////////////////////////////////////////////////////////
        public SpriteBatch Sprite
        {
            get { return _sprite; }
            set { _sprite = value; }
        }

        /////////////////////////////////////////////////////////////////////
        public override void Update(GameTime gameTime, KeyboardState keyboardState)
        {

        }

        /////////////////////////////////////////////////////////////////////
        public override void Draw()
        {
            if (IsActive)
            {
                if (_sprite != null && _fontSprite != null && _text != null)
                {
                    _sprite.Begin();
                    _sprite.Draw(ContentLoader.ContentTextures[TextureEnum.BackgroundLevel1], Vector2.Zero, Color.White);
                    _sprite.DrawString(_fontSprite, _text, _position, new Color(255, 255, 255, 64));
                    _sprite.End();
                }
            }
        }

        /////////////////////////////////////////////////////////////////////
        public override void Start()
        {
            //_fieldControlLoaderThread.Start();
            base.Start();

            AsteroidFieldControl asteroidFieldManager = new AsteroidFieldControl(3);
            asteroidFieldManager.Add(new AsteroidField(Device.Viewport.Width, Device.Viewport.Height, ContentManager, Sprite, 0.25f, 12, 25));
            asteroidFieldManager.Add(new AsteroidField(Device.Viewport.Width, Device.Viewport.Height, ContentManager, Sprite, 0.4f, 8, 50));
            asteroidFieldManager.Add(new AsteroidField(Device.Viewport.Width, Device.Viewport.Height, ContentManager, Sprite, 0.65f, 5, 75));
            ObjectsContainer.Instance.Add(ObjectType.AsteroidFields, asteroidFieldManager);

            AsyncWorker.Instance.AsyncCall(asteroidFieldManager.InitFields);
            AsyncWorker.Instance.OnAsyncCallComplete += new AsyncWorker.AsyncCallCompleted(Instance_OnAsyncCallMethodComplete);

        }

        /////////////////////////////////////////////////////////////////////
        private void Instance_OnAsyncCallMethodComplete(object sender, AsyncCompleteEventArgs eventArgs)
        {
            Finish();
        }

        /////////////////////////////////////////////////////////////////////
        public override void Finish()
        {
            if(base.NextScene != null)
            {
                base.NextScene.SwitchOnFadeIn = true;
            }
            base.Finish();
        }
    }
/////////////////////////////////////////////////////////////////////////////
}//FreeSpace.Scenes
